package com.cldz.utils;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 13:22 2018/12/14
 * @ Description：
 * @ Modified By：
 */
public class TimeBetUtil {

    public static final String PATTERN_YM = "yyyyMM"; // pattern_ym
//    public static final String pattern_ymd = "yyyyMM dd"; // pattern_ymd
    public static final String PATTERN_YMD = "yyyyMMdd";


    public static List<String> getYears(String start, String end) {
        List<String> dateList = new ArrayList<>();
        if (StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)) {
            Date dBegin = TimeBetUtil.parse(start, TimeBetUtil.PATTERN_YM);
            Date dEnd = TimeBetUtil.parse(end, TimeBetUtil.PATTERN_YM);

            Calendar calBegin = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calBegin.setTime(dBegin);
            int year1 = calBegin.get(Calendar.YEAR);
            Calendar calEnd = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calEnd.setTime(dEnd);
            int year2 = calEnd.get(Calendar.YEAR);
            // 测试此日期是否在指定日期之后
            while (year2 >= year1) {
                dateList.add(TimeBetUtil.format(calBegin.getTime(), "yyyy"));
                calBegin.add(Calendar.YEAR, 1);
                year1 = calBegin.get(Calendar.YEAR);
            }
        }
        return dateList;
    }


    public static List<String> getMonths(String start, String end) {
        List<String> dateList = new ArrayList<String>();
        if (StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)) {
            Date dBegin = TimeBetUtil.parse(start, TimeBetUtil.PATTERN_YM);
            Date dEnd = TimeBetUtil.parse(end, TimeBetUtil.PATTERN_YM);

            Calendar calBegin = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calBegin.setTime(dBegin);
            int yearMonth1 = (calBegin.get(Calendar.YEAR)) * 100 + calBegin.get(Calendar.MONTH);
            Calendar calEnd = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calEnd.setTime(dEnd);
            int yearMonth2 = (calEnd.get(Calendar.YEAR)) * 100 + calEnd.get(Calendar.MONTH);
            // 测试此日期是否在指定日期之后
            while (yearMonth2 >= yearMonth1) {
                dateList.add(TimeBetUtil.format(calBegin.getTime(), TimeBetUtil.PATTERN_YM));
                calBegin.add(Calendar.MONTH, 1);
                yearMonth1 = (calBegin.get(Calendar.YEAR)) * 100 + calBegin.get(Calendar.MONTH);
            }
        }
        return dateList;

    }
    public static List<String> getDays(String startDate, String endDate){

        SimpleDateFormat format = new SimpleDateFormat(PATTERN_YMD);
        List<String> result = new ArrayList<>();
        try{
            Calendar calBegin = Calendar.getInstance();
            
            Date begin = format.parse(startDate);
            calBegin.setTime(begin);
            Calendar calEnd = Calendar.getInstance();
            
            Date end = format.parse(endDate);
            
            calEnd.setTime(end);
            
            while (end.after(calBegin.getTime())){
                calBegin.add(Calendar.DAY_OF_MONTH,1);
                result.add(format.format(calBegin.getTime()));
            }
            
        }catch (Exception e){
            
        }
        return result;
    }



    public static String format(Date date, String pattern) {
        DateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static Date parse(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static void main(String[] args) {
        List<String> years = TimeBetUtil.getYears("201501", "201802");
        List<String> months = TimeBetUtil.getMonths("201501", "201802");

        List<String> days = TimeBetUtil.getDays("20181124", "20190112");
        System.out.println();
    }
}
